package com.xiongkw.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MongoConfig {
//    private String hosts="mongo-mongo.int.xiaohongshu.com";
//    private Integer port=27017;
//    private String db="sms";
//    private String username="sit-user";
//    private String password="cDk%KVJA4qA5";
//    private String authSource="admin";

    private String[] hosts={"10.248.224.3","10.248.224.3","10.248.224.3"};
    private Integer[] ports={33101,33102,33103};
    private String db="hrs_feature";
    private String username="openteam";
    private String password="7l299l6BzAr^S15";
    private String authSource="hrs_feature";

	public MongoClient getMongoClent(){
        MongoCredential scramSha1Credential= MongoCredential.createScramSha1Credential(username, authSource, password.toCharArray());
        MongoClient mongoClient =null;
        if (hosts.length > 1) {
            List<ServerAddress> addresses = new ArrayList<>(hosts.length);
            List<MongoCredential> credentialsList = new ArrayList<>(hosts.length);
            for (int i = 0;i < ports.length && i < hosts.length;i++) {

                addresses.add(new ServerAddress(hosts[i], ports[i]));
                credentialsList.add(scramSha1Credential);
            }
            mongoClient = new MongoClient(addresses, credentialsList);
        } else {
            mongoClient = new MongoClient(new ServerAddress(hosts[0], ports[0]), Arrays.asList(scramSha1Credential));
        }
        return mongoClient;
	}
	
	public MongoDatabase getMongoDatabase(){

	    return this.getMongoClent().getDatabase(db);
	}
}
